import request from '@/utils/request/doRequest'
import authority from '@/utils/authority'

export default async ({ encrypted_data, iv }) => {
  const { session_key, unionid, openid } = authority.get() || {}
  const { user, token, visitor } = await request({
    url: '/api/app/v1/miniapp/login',
    method: 'post',
    data: {
      session_key,
      encrypted_data,
      iv,
      unionid,
      openid
    }
  })
  if (user) {
    user.visitor = visitor
    user.token = token
    authority.set(user)
    return user
  } else {
    return Promise.reject(new Error('登陆失败'))
  }
}
